﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CloudFranceTriple.Models;

namespace CloudFranceTriple.WebRole.Areas.Forum.Controllers
{
    [CloudFranceTriple.WebRole.Filters.InitializeSimpleMembership]
    public class PrivateMessageReadController : Controller
    {
        private cloudfrAboq2RsDMEntities db = new cloudfrAboq2RsDMEntities();

        //
        // GET: /Forum/PrivateMessageRead/Create

        [ChildActionOnly]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember")]
        public ActionResult Create(Int32 id = 0, Int32 id2 = 0)
        {
            // Ne marquer que les messages dont le lecteur est destinataire et non pas l'auteur.
            if(db.PrivateMessageBoxSet.Find(id).ToPrivateMessage!=id2)return null;

            // Ne marquer chaque message privé lu qu'une seule fois.
            if (db.PrivateMessageReadSet.Where(read => read.PrivateMessageBox_Id == id).Where(read2 => read2.UserId == id2).Count() > 0) return null;

            var privatemessagereadset = new PrivateMessageReadSet() { PrivateMessageBox_Id = id, UserId = id2 };
            db.PrivateMessageReadSet.Add(privatemessagereadset);
            db.SaveChanges();
            return null;
        }

        //
        // POST: /Forum/PrivateMessageRead/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [ChildActionOnly]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember")]
        public ActionResult DeleteConfirmed(int id)
        {
            PrivateMessageReadSet privatemessagereadset = db.PrivateMessageReadSet.Find(id);
            db.PrivateMessageReadSet.Remove(privatemessagereadset);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}